<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Service\Curl;
use GatewayClient\Gateway;


class CtsController extends Controller
{

    // 财通社采集测试
    public function initialize()
    {
        parent::initialize();
    }

    // 将文章保存到财通社（对接财通社管理后台保存数据的接口：用于采集的公众号文章保存到财通社专栏）
    public function to()
    {
        // 参数
        $uid    = 564;
        $author = '财通社';
        $title  = '总经理刚套现近700万，实控人、前董事长此前均被立案';
        $cover  = 'https://slh.caitongnews.com/images/2023/07/05/64a5575ce62d1.jpg';
        $desc   = '<p>详情</p>';

        // 组合
        $cookie = 'PHPSESSID=l64e9eetv265acqfoidv9krui2; aid=1; token=097eef40487211b5723e369d55bc2b48';
        $ctsUrl = 'https://plat.caitongnews.cn/API/Archive/saveArchive';
        $header = ['Cookie:'.$cookie, 'Content-Type:multipart/form-data'];
        $header = ['Cookie:'.$cookie, 'Content-Type:application/json'];
        $param  = ['badge' => 'normal', 'category' => 'article', 'status' => 'enabled', 'author' => $author, 'original' => $author, 'cover' => $cover, 'desc' => $desc, 'subject' => $title, 'tags' => $title, 'uid' => $uid];

        $curl = new Curl();
        $res  = $curl->url($ctsUrl)->header($header)->json($param)->request();
        $res  = json_decode($res, true);
        if($res['code'] = '0000'){
            return $this->success($res['msg']);
        } else{
            return $this->error($res['msg']);
        }
    }

    // 远程服务器请求
    // 请求财通社接口：微信群发
    public function wechat()
    {
        // 参数
        $title = 'fffff总经理刚套现近700万，实控人、前董事长此前均被立案';
        $link  = 'https://www.baidu.com/';
        $weIds = [61];

        // 复旦复华测试
        $title = '欢迎加入复旦复华舆情监控群！为了更好体验，请将本群置顶，并打开新消息提示，如需要打开相关海外链接，可能需要使用特殊设置。';
        $link  = '';
        $weIds = [65];  // 复旦复华

        // 组合
        $cookie = 'PHPSESSID=l64e9eetv265acqfoidv9krui2; aid=1; token=097eef40487211b5723e369d55bc2b48';
        $ctsUrl = 'https://plat.caitongnews.cn/API/Archive/sendWechat';
        $header = ['Cookie:'.$cookie, 'Content-Type:multipart/form-data'];
        $header = ['Cookie:'.$cookie, 'Content-Type:application/json'];
        $param  = ['subject' => $title, 'wechat_id' => $weIds, 'link' => $link];

        $curl = new Curl();
        $res  = $curl->url($ctsUrl)->header($header)->json($param)->request();
        $res  = json_decode($res, true);
        if($res){
            if($res['code'] = '0000'){
                return $this->success($res['msg']);
            } else{
                return $this->error($res['msg']);
            }
        }

        return $this->success();
    }

    // 本地服务器
    // 本台服务器执行微信群发
    public function sendWechat()
    {
        // 参数
        $title = 'fffff总经理刚套现近700万，实控人、前董事长此前均被立案';
        $link  = 'https://www.baidu.com/';
        $weIds = [65];  // 复旦复华

        //
        $title = '欢迎加入复旦复华舆情监控群！为了更好体验，请将本群置顶，并打开新消息提示，如需要打开相关海外链接，可能需要使用特殊设置。';
        $link  = '';
        $weIds = [65];  // 复旦复华

        // 微信群发
        $Gateway = new Gateway("127.0.0.1:6238");
        $content = $title.chr(10).chr(13).$link;
        for($i = 0; $i < count($weIds); $i++){
            $post = ["type" => "WeChatMessage", "msg" => $content, "category" => "article", "topic_id" => 0, "topic_ids" => [], "archive_id" => 0, "wechat_id" => $weIds[$i]];
            $Gateway->sendToAll(json_encode($post));
        }

        return $this->success();
    }

    // 用户整理
    public function user2()
    {
        $news = [];
        $user = [
            ['id' => '134', 'name' => '深蓝财经',],
            ['id' => '135', 'name' => '易简财经',],
            ['id' => '137', 'name' => '金角财经',],
            ['id' => '138', 'name' => '面包财经',],
            ['id' => '139', 'name' => '看财经',],
            ['id' => '140', 'name' => '基岩资本',],
            ['id' => '141', 'name' => '愉见财经',],
            ['id' => '178', 'name' => '富凯财经',],
            ['id' => '183', 'name' => '证通社',],
            ['id' => '184', 'name' => '美好商业共同体',],
            ['id' => '185', 'name' => '粤开奇霖研究',],
            ['id' => '186', 'name' => '并购优塾',],
            ['id' => '203', 'name' => '研报哥',],
            ['id' => '204', 'name' => 'A股你莫愁',],
            ['id' => '210', 'name' => '樱桃大房子',],
            ['id' => '211', 'name' => '德林社',],
            ['id' => '212', 'name' => '公子豹资本圈',],
            ['id' => '216', 'name' => 'A股题材宝',],
            ['id' => '217', 'name' => '国际投行研究报告',],
            ['id' => '221', 'name' => '全球直通车',],
            ['id' => '224', 'name' => '明察宏观',],
            ['id' => '226', 'name' => '杨德龙宏观策略研究',],
            ['id' => '227', 'name' => '野马财经',],
            ['id' => '228', 'name' => '金证研',],
            ['id' => '229', 'name' => '韩迅乱谈',],
            ['id' => '230', 'name' => '财信证券伍超明',],
            ['id' => '232', 'name' => '桑葛石风险简报',],
            ['id' => '233', 'name' => '一合财经',],
            ['id' => '234', 'name' => '市值风云',],
            ['id' => '239', 'name' => '私募排排网',],
            ['id' => '244', 'name' => '香港国际新经济研究院',],
            ['id' => '260', 'name' => '图灵智物',],
            ['id' => '263', 'name' => '财经无忌',],
            ['id' => '289', 'name' => '岑智勇',],
            ['id' => '290', 'name' => 'A股老玩童',],
            ['id' => '291', 'name' => '环球老虎财经',],
            ['id' => '294', 'name' => '梧桐树下V',],
            ['id' => '322', 'name' => '格隆汇',],
            ['id' => '348', 'name' => '先机财经',],
            ['id' => '389', 'name' => '富鱼多多',],
            ['id' => '391', 'name' => '董秘学苑',],
            ['id' => '399', 'name' => '贝瑞研究',],
            ['id' => '404', 'name' => '郭士英',],
            ['id' => '449', 'name' => '泽平宏观',],
            ['id' => '468', 'name' => '马靖昊说会计',],
            ['id' => '564', 'name' => '财通社',],
            ['id' => '603', 'name' => '光伏新观察',],
            ['id' => '609', 'name' => '藏金洞',],
            ['id' => '658', 'name' => '智物科技评论',],
            ['id' => '699', 'name' => 'SMDC科创数据',],
            ['id' => '720', 'name' => '估值之家',],
            ['id' => '736', 'name' => '港股研究社',],
            ['id' => '737', 'name' => '美股研究社',],
            ['id' => '738', 'name' => '金融界',],
            ['id' => '739', 'name' => '凤凰网房产',],
            ['id' => '740', 'name' => '财视传媒',],
            ['id' => '741', 'name' => '潮汐商业评论',],
            ['id' => '742', 'name' => '美股滚雪球',],
            ['id' => '786', 'name' => '市值观察',],
            ['id' => '829', 'name' => '李大霄',],
            ['id' => '846', 'name' => '亚洲科技电讯',],
            ['id' => '863', 'name' => '赶碳号科技',],
            ['id' => '996', 'name' => '时代周报',],
            ['id' => '997', 'name' => '19号商研社',],
            ['id' => '999', 'name' => '时代财经APP',],
            ['id' => '1000', 'name' => '猛犸资本局',],
            ['id' => '1001', 'name' => '读城记工作室',],
            ['id' => '1005', 'name' => 'ThirdBridge',],
        ];

        $users = [
            '10195' => ['id' => '134', 'name' => '深蓝财经',],
            '10194' => ['id' => '135', 'name' => '易简财经',],
            '10193' => ['id' => '137', 'name' => '金角财经',],
            '10192' => ['id' => '138', 'name' => '面包财经',],
            '10191' => ['id' => '139', 'name' => '看财经',],
            '10190' => ['id' => '140', 'name' => '基岩资本',],
            '10189' => ['id' => '141', 'name' => '愉见财经',],
            '10188' => ['id' => '178', 'name' => '富凯财经',],
            '10187' => ['id' => '183', 'name' => '证通社',],
            '10186' => ['id' => '184', 'name' => '美好商业共同体',],
            '10185' => ['id' => '185', 'name' => '粤开奇霖研究',],
            '10184' => ['id' => '186', 'name' => '并购优塾',],
            '10183' => ['id' => '203', 'name' => '研报哥',],
            '10182' => ['id' => '204', 'name' => 'A股你莫愁',],
            '10181' => ['id' => '210', 'name' => '樱桃大房子',],
            '10180' => ['id' => '211', 'name' => '德林社',],
            '10179' => ['id' => '212', 'name' => '公子豹资本圈',],
            '10178' => ['id' => '216', 'name' => 'A股题材宝',],
            '10177' => ['id' => '217', 'name' => '国际投行研究报告',],
            '10176' => ['id' => '221', 'name' => '全球直通车',],
            '10175' => ['id' => '224', 'name' => '明察宏观',],
            '10174' => ['id' => '226', 'name' => '杨德龙宏观策略研究',],
            '10173' => ['id' => '227', 'name' => '野马财经',],
            '10172' => ['id' => '228', 'name' => '金证研',],
            '10171' => ['id' => '229', 'name' => '韩迅乱谈',],
            '10170' => ['id' => '230', 'name' => '财信证券伍超明',],
            '10169' => ['id' => '232', 'name' => '桑葛石风险简报',],
            '10168' => ['id' => '233', 'name' => '一合财经',],
            '10167' => ['id' => '234', 'name' => '市值风云',],
            '10166' => ['id' => '239', 'name' => '私募排排网',],
            '10165' => ['id' => '244', 'name' => '香港国际新经济研究院',],
            '10164' => ['id' => '260', 'name' => '图灵智物',],
            '10163' => ['id' => '263', 'name' => '财经无忌',],
            '10162' => ['id' => '289', 'name' => '岑智勇',],
            '10161' => ['id' => '290', 'name' => 'A股老玩童',],
            '10160' => ['id' => '291', 'name' => '环球老虎财经',],
            '10159' => ['id' => '294', 'name' => '梧桐树下V',],
            '10158' => ['id' => '322', 'name' => '格隆汇',],
            '10157' => ['id' => '348', 'name' => '先机财经',],
            '10156' => ['id' => '389', 'name' => '富鱼多多',],
            '10155' => ['id' => '391', 'name' => '董秘学苑',],
            '10154' => ['id' => '399', 'name' => '贝瑞研究',],
            '10153' => ['id' => '404', 'name' => '郭士英',],
            '10152' => ['id' => '449', 'name' => '泽平宏观',],
            '10151' => ['id' => '468', 'name' => '马靖昊说会计',],
            '10150' => ['id' => '564', 'name' => '财通社',],
            '10149' => ['id' => '603', 'name' => '光伏新观察',],
            '10148' => ['id' => '609', 'name' => '藏金洞',],
            '10147' => ['id' => '658', 'name' => '智物科技评论',],
            '10146' => ['id' => '699', 'name' => 'SMDC科创数据',],
            '10145' => ['id' => '720', 'name' => '估值之家',],
            '10144' => ['id' => '736', 'name' => '港股研究社',],
            '10143' => ['id' => '737', 'name' => '美股研究社',],
            '10142' => ['id' => '738', 'name' => '金融界',],
            '10141' => ['id' => '739', 'name' => '凤凰网房产',],
            '10140' => ['id' => '740', 'name' => '财视传媒',],
            '10139' => ['id' => '741', 'name' => '潮汐商业评论',],
            '10138' => ['id' => '742', 'name' => '美股滚雪球',],
            '10137' => ['id' => '786', 'name' => '市值观察',],
            '10136' => ['id' => '829', 'name' => '李大霄',],
            '10135' => ['id' => '846', 'name' => '亚洲科技电讯',],
            '10134' => ['id' => '863', 'name' => '赶碳号科技',],
            '10133' => ['id' => '996', 'name' => '时代周报',],
            '10132' => ['id' => '997', 'name' => '19号商研社',],
            '10131' => ['id' => '999', 'name' => '时代财经APP',],
            '10130' => ['id' => '1000', 'name' => '猛犸资本局',],
            '10129' => ['id' => '1001', 'name' => '读城记工作室',],
            '10128' => ['id' => '1005', 'name' => 'ThirdBridge',],
        ];

        // 获取深蓝汇公众号用户
        $lists = (new User())->where('id', '>=', 10128)->get()->toArray();
        $lists = array_column($lists, 'id', 'realname');

        // 匹配用户
        foreach($user as $key => $item){
            $_name = $item['name'];
            $_id   = $lists[$_name];
            // 组合
            $news[$_id] = $item;
        }

        return $this->showArray($news, true);
    }

    public function user()
    {

        $string = '134		深蓝财经
135		易简财经
137		金角财经
138		面包财经
139		看财经
140		基岩资本
141		愉见财经
178		富凯财经
183		证通社
184		美好商业共同体
185		粤开奇霖研究
186		并购优塾
203		研报哥
204		A股你莫愁
210		樱桃大房子
211		德林社
212		公子豹资本圈
216		A股题材宝
217		国际投行研究报告
221		全球直通车
224		明察宏观
226		杨德龙宏观策略研究
227		野马财经
228		金证研
229		韩迅乱谈
230		财信证券伍超明
232		桑葛石风险简报
233		一合财经
234		市值风云
239		私募排排网
244		香港国际新经济研究院
260		图灵智物
263		财经无忌
289		岑智勇
290		A股老玩童
291		环球老虎财经
294		梧桐树下V
322		格隆汇
348		先机财经
389		富鱼多多
391		董秘学苑
399		贝瑞研究
404		郭士英
449		泽平宏观
468		 马靖昊说会计
564		财通社
603		光伏新观察
609		藏金洞
658		智物科技评论
699		 SMDC科创数据
720		 估值之家
736		港股研究社
737		美股研究社
738		金融界
739		凤凰网房产
740		财视传媒
741		潮汐商业评论
742		美股滚雪球
786		市值观察
829		李大霄
846		亚洲科技电讯
863		赶碳号科技
996		时代周报
997		19号商研社
999		时代财经APP
1000		猛犸资本局
1001		读城记工作室
1005		ThirdBridge';
        $all    = explode(chr(10), $string);
        $users  = [];
        foreach($all as $key => $vv){
            $_item   = explode('		', $vv);
            $users[] = ['id' => intval($_item[0]), 'name' => trim($_item[1])];
        }

        return $this->showArray($users, false);
    }

    // 展示 数组
    protected function showArray($array = [], $show_index = true, $is_cline = true)
    {
        if(empty($array)){
            return "[]";
        }

        $cline  = '<br>';
        $string = "";
        $count  = count($array);
        foreach($array as $key => $vv){
            $_row = '';
            if($show_index){
                $_row .= "'".$key."'  =>  ";
            }
            if(is_array($vv)){
                $_row .= $this->showArray($vv, true, false);
            } else{
                $_row .= "'".$vv."'";
            }

            // 展示
            $string .= $_row;
            $string .= ',';
            if($count - 1 > $key){
                if($is_cline){
                    $string .= $cline;
                }
            }
        }


        return "[".$string."]";
    }
}



